package com.boarsoft.boar.code.mac.flow.generator;

import java.util.ArrayList;
import java.util.List;

import com.boarsoft.flow.common.bean.CommonWrapConfInfo;
import com.boarsoft.flow.common.bean.CommonWrapHandlderConfInfo;
import com.boarsoft.flow.common.bean.CommonWrapMethodConfInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import com.boarsoft.boar.code.bean.biz.BeanMappingBiz;
import com.boarsoft.boar.code.entity.BeanMapping;
import com.boarsoft.boar.code.entity.WrapHandlerInfo;
import com.boarsoft.boar.code.mac.flow.biz.WrapHandlerInfoBiz;
import com.boarsoft.common.util.JsonUtil;

@Component
public class WrapMappingGenerator {

	private static final String CHECK_IN_NO = "0";
	private static final String CHECK_OUT_NO = "1";

	@Autowired
	private WrapHandlerInfoBiz wrapHandlerInfoBiz;

	@Autowired
	private BeanMappingBiz beanMappingBiz;

	public String generate(String code, String key, int type, String appId) {
		CommonWrapHandlderConfInfo commonWrapHandlderConfInfo = new CommonWrapHandlderConfInfo();
		commonWrapHandlderConfInfo.setKey(key);

		List<CommonWrapMethodConfInfo> methodConfList = new ArrayList<>();

		List<WrapHandlerInfo> list = wrapHandlerInfoBiz.find(code, key, type, appId);
		if (CollectionUtils.isEmpty(list)) {
			return "";
		}

		String id = list.get(0).getId();

		// checkIn方法处理
		process(methodConfList, id, CHECK_IN_NO);

		// checkOut方法处理
		process(methodConfList, id, CHECK_OUT_NO);

		commonWrapHandlderConfInfo.setCommonWrapMethodConfInfoList(methodConfList);

		return JsonUtil.toJSONString(commonWrapHandlderConfInfo);
	}

	private void process(List<CommonWrapMethodConfInfo> methodConfList, String id, String methodType) {
		List<BeanMapping> beanMappings = beanMappingBiz.listFromCodeId(id, methodType);
		if (beanMappings != null && beanMappings.size() > 0) {
			CommonWrapMethodConfInfo commonWrapMethodConfInfo = new CommonWrapMethodConfInfo();
			if (CHECK_IN_NO.equals(methodType)) {
				commonWrapMethodConfInfo.setMethodName("checkIn");
			} else {
				commonWrapMethodConfInfo.setMethodName("checkOut");
			}

			List<CommonWrapConfInfo> checkInList = new ArrayList<>();
			for (BeanMapping beanMapping : beanMappings) {
				CommonWrapConfInfo commonWrapConfInfo = new CommonWrapConfInfo();

				commonWrapConfInfo.setFromObj(beanMapping.getFromObj());
				commonWrapConfInfo.setFromProp(beanMapping.getFromProp());
				commonWrapConfInfo.setFromPropType(beanMapping.getFromPropType());
				commonWrapConfInfo.setFromType(beanMapping.getFromType());
				commonWrapConfInfo.setToObj(beanMapping.getToObj());
				commonWrapConfInfo.setToProp(beanMapping.getToProp());
				commonWrapConfInfo.setToPropType(beanMapping.getToPropType());
				commonWrapConfInfo.setToType(beanMapping.getToType());

				checkInList.add(commonWrapConfInfo);
			}

			if (checkInList.size() > 0) {
				commonWrapMethodConfInfo.setCommonWrapConfInfoList(checkInList);
				methodConfList.add(commonWrapMethodConfInfo);
			}
		}
	}
}
